package com.cyf.db.mapper;

import com.cyf.db.bean.AppCar;
import com.cyf.wechat.network.bean.AppCarRsp;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * app用户注册的车辆
 * Created by cage on 2017/2/16.
 */

public interface AppCarMapper {

    @Select("select * from app_car where plate = #{plate} limit 0,1")
    AppCar getByPlate(@Param("plate") String plate);

    @Select(" SELECT us.mobile FROM app_car AS c LEFT JOIN app_usercar AS uc ON c.id = uc.carid " +
            " LEFT JOIN users AS us ON uc.userid = us.id " +
            " WHERE c.id = #{carId} LIMIT 0,1 ")
    String getPlateByMobile(@Param("carId") Integer carId);

    @Select("select * from app_car where id = #{id} order by id desc limit 1")
    AppCar getById(@Param("id") Integer id);

    @Select("select id,plate from app_car where userId = #{userId}")
    List<AppCarRsp> findAppCarListByUserId(@Param("userId") Integer userId);

    @Select("select * from app_car where plate = #{plate} and userId = #{userId} limit 0,1")
    AppCar getByPlateUserId(@Param("plate") String plate,@Param("userId") Integer userId);


    @Insert("insert into app_car(plate, type, color, brand,mileage,checkTime,registerTime,isEnergy,userId,createTime,carcode,cardrive) values(#{appCar.plate}, #{appCar.type}, #{appCar.color}, #{appCar.brand},#{appCar.mileage},#{appCar.checkTime},#{appCar.registerTime},#{appCar.isEnergy},#{appCar.userId},#{appCar.createTime},#{appCar.carcode},#{appCar.cardrive})")
    @SelectKey(statement = "SELECT LAST_INSERT_ID()", keyProperty = "appCar.id", resultType = int.class, before = false)
    void insertAppCar(@Param("appCar") AppCar appCar);

    @Update("update app_car set carcode = #{appCar.carcode}, cardrive = #{appCar.cardrive} where id = #{appCar.id}")
    void updateAuthInfo(@Param("appCar") AppCar appCar);

    @Update("update app_car set type = #{appCar.type}, color = #{appCar.color}, brand = #{appCar.brand},mileage = #{appCar.mileage}, checkTime = #{appCar.checkTime}, registerTime = #{appCar.registerTime},isEnergy = #{appCar.isEnergy} where id = #{appCar.id}")
    void updateAppCar(@Param("appCar") AppCar appCar);


    @Update("update app_car set  plate = #{c.plate},type = #{c.type},brand = #{c.brand},carcode = #{c.carcode}," +
            " cardrive = #{c.cardrive},mileage = #{c.mileage},checkTime = #{c.checkTime},registerTime = #{c.registerTime},isEnergy = #{c.isEnergy}  where id = #{c.id}")
    void updateAppCarById(@Param("c") AppCar c);

    @Delete("delete from app_car where id = #{id}")
    void deleteAppCarById(@Param("id") Integer id);

    @Select("select * from app_car where plate = #{plate} and id != #{id} limit 0,1")
    AppCar getByPlateAndId(@Param("plate") String plate,@Param("id") Integer id);
}
